package com.xiaodao.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class TestController {
	
    @Autowired
    private RestTemplate restTemplate;
	@Autowired
	private LoadBalancerClient loadBalancerClient;//ribbon 负载均衡客户端

	@GetMapping("/add")
	public String add(Integer a, Integer b) {
		ServiceInstance si=loadBalancerClient.choose("CLIENT-A");
		StringBuffer sb=new StringBuffer("");
		sb.append("http://");
		sb.append(si.getHost());
		sb.append(":");
		sb.append(si.getPort());
		sb.append("/add?a="+a+"&b="+b);
		System.out.println(sb.toString());
		String result = restTemplate.getForObject(sb.toString(), String.class);
//		String result = restTemplate
//				.getForObject("http://CLIENT-A/add?a=" + a + "&b=" + b, String.class);
		System.out.println(result);
		return result;
	}
}